package p.sunmes.les.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import p.sunmes.les.managers.CM;

public class DataUtil {

	public static final String ENCODING = "UTF-8";

	public static List<Map<String, String>> read(InputStream in) {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();

		BufferedReader br = null;
		if (in == null) {
			return list;
		}
		try {
			br = new BufferedReader(new InputStreamReader(in, ENCODING));
			String s = br.readLine();
			if (s != null) {
				String[] fields = s.split("\t");
				while ((s = br.readLine()) != null) {
					list.add(readString(s, fields));
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return list;
	}

	public static byte[] getStringByte(String text) {
		byte[] data = null;
		try {
			data = text.getBytes(ENCODING);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return data;
	}

	public static Map<String, String> readString(String string,
			String[] fields) {
		Map<String, String> map = new HashMap<String, String>();
		String[] ss = string.split("\t");
		for (int i = 0; i < fields.length && i < ss.length; i++) {
			map.put(fields[i], ss[i]);
		}
		return map;
	}

	public static int byte2UnsignInt(int i) {
		i = i & 0xFF;
		return i;
	}

	public static void encodeFile(String file, String outFile) {
		InputStream is = null;
		OutputStream os = null;

		try {
			is = new FileInputStream(file);
			//CheckBOM.
			byte[] bom = new byte[CM.UTF8BOM.length];
			is.read(bom, 0, bom.length);
			boolean isHasBom = true;
			for (int i = 0; i < bom.length; i++) {
				if (bom[i] != CM.UTF8BOM[i]) {
					isHasBom = false;
					break;
				}
			}
			if (!isHasBom) {
				is.close();
				is = new FileInputStream(file);
			}

			os = new DataEncodeOutputStream(new FileOutputStream(outFile));
			byte[] buff = new byte[1024];
			int len;
			while ((len = is.read(buff)) > 0) {
				os.write(buff, 0, len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static String decodeFile(String file) {
		BufferedReader br = null;
		StringBuffer sb = new StringBuffer();
		try {
			br = new BufferedReader(new InputStreamReader(
					new DataDecodeInputStream(new FileInputStream(file)),
					"UTF-8"));

			String line;
			while ((line = br.readLine()) != null) {
				sb.append(line).append("\n");
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return sb.toString();
	}

	public static void encodeFile(File file) {
		if (file.getName().endsWith(".txt")) {
			System.out.println("EncodeFile:" + file.getName());
			String path = file.getAbsolutePath();
			//				String outPath = String.format("%s.dat",
			//						path.substring(0, path.length() - 4));
			String outPath = path + "e";
			encodeFile(path, outPath);
			String check = decodeFile(outPath);
			System.out.println("DecodeBack:");
			System.out.println(check);
			System.out.println();
		}
	}

	public static void encodeFiles(String dirPath) {
		File dir = new File(dirPath);
		if (dir.isDirectory()) {
			File[] files = dir.listFiles();
			for (File file : files) {
				encodeFile(file);
			}
		} else {
			encodeFile(dir);
		}
	}

	public static void main(String[] args) {
		//		t1();
		//		String dir = "E:\\LibGdxProject\\F6C\\android\\assets\\config";
		//		encodeFiles(dir);

		//		System.out.println(byte2UnsignInt(-1));
		//		System.out.println((byte) 255);

		if (args.length == 1) {
			encodeFiles(args[0]);
		}
	}

	public static void t1() {
		String file = "E:\\LibGDXPros\\XXLPro\\android\\assets\\config\\weapon.txt";
		String outFile = "E:\\LibGDXPros\\XXLPro\\android\\assets\\config\\weapon.dat";
		encodeFile(file, outFile);
		String fileText = decodeFile(outFile);

		List<String> list = getLineString(fileText);
		for (String s : list) {
			System.out.println(s);
		}
	}

	public static List<String> getLineString(String text) {
		List<String> list = new ArrayList<String>();
		int s = 0, lastS = 0;
		while ((s = text.indexOf('\n', lastS)) >= 0) {
			if (text.charAt(s - 1) == '\r') {
				list.add(text.substring(lastS, s - 1));
			} else {
				list.add(text.substring(lastS, s));
			}
			lastS = s + 1;
		}
		list.add(text.substring(lastS));
		return list;
	}
}
